package base;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


import objects.Release;
import objects.ReleaseChecker;

import monitor.IRCHook;
import monitor.WebsiteThread;

public class Manager extends Thread {
	
	Display _Disp;
	private WebsiteThread _WebThread;
	private IRCHook _IRCThread;
	private ReleaseChecker _RLSCHK;
	private List<Release> _RlsToBeChecked;
	private List<String> _SnatchedReleases;
	private List<String> _EXCLUDEDRELEASES;

	public Manager(Display d){
		
		_RlsToBeChecked = Collections.synchronizedList(new ArrayList<Release>());
		_SnatchedReleases = Collections.synchronizedList(new ArrayList<String>());
		_EXCLUDEDRELEASES = Collections.synchronizedList(new ArrayList<String>());
		
		_Disp = d;
		
	}
	
	public void run(){
		while(true){
			synchronized(_RlsToBeChecked){
				while(!_RlsToBeChecked.isEmpty()){
					for(Release CurrentRLS : _RlsToBeChecked){
						CheckRLS(CurrentRLS);
					}
				
				}	
			}
		}
		
	}
	
	public synchronized void AddRLS(Release R){
		if(_SnatchedReleases.contains(R.getName()) 
				|| _EXCLUDEDRELEASES.contains(R.getName())){
			_Disp.DisplayString(R.getName() + " Already snatched, or excluded, not re-added.");
			return;
		}else{
			_Disp.DisplayString(R.getName() + " Added to snatch queue.");
			_RlsToBeChecked.add(R);
		}
	}
	
	public synchronized void CheckRLS(Release R){
		if(_RLSCHK.CheckRelease(R)){
			SnatchRelease(R);
		}else{
			
		}
	}
	
	private synchronized void SnatchRelease(Release R){
		
		_RlsToBeChecked.remove(R);
	}
	
}
